package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Text;
import com.ruoyi.system.service.ITextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文案信息Controller
 *
 * @author ...
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/system/text")
public class TextController extends BaseController {
    @Autowired
    private ITextService textService;

    /**
     * 查询文案信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Text text) {
        startPage();
        List<Text> list = textService.selectTextList(text);
        return getDataTable(list);
    }

    /**
     * 根据标题查询
     * @param title
     * @return
     */
    @GetMapping("/title")
    public AjaxResult getOne(@RequestParam("title") String title){
        Text text = textService.getOneByTitle(title);
        return AjaxResult.success(text);
    }

    /**
     * 获取文案信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(textService.selectTextById(id));
    }

    /**
     * 新增文案信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody Text text) {
        Text query =new Text();
        query.setTitle(text.getTitle());
        List<Text> texts = textService.selectTextList(query);
        Assert.isTrue(texts.size()==0,"该标题已存在");
        text.setCreateUser(SecurityUtils.getUserId().intValue());
        text.setCreateTime(DateUtils.getNowDate());
        return toAjax(textService.insertText(text));
    }

    /**
     * 修改文案信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Text text) {
        Text query =new Text();
        query.setTitle(text.getTitle());
        List<Text> texts = textService.selectTextList(query);
        Assert.isTrue(texts.size()==0,"该标题已存在");
        text.setUpdateUser(SecurityUtils.getUserId().intValue());
        text.setUpdateTime(DateUtils.getNowDate());
        return toAjax(textService.updateText(text));
    }

    /**
     * 删除文案信息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(textService.deleteTextByIds(ids));
    }
}
